﻿using System;
using System.Linq;
using System.Xml.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace SharePointUtilitiesForDevelopers.Tests
{
    /// <summary>
    /// Test class for <see cref="SharePointQueryOrderByClauseExtensionMethods"/>.
    /// </summary>
    [TestClass]
    public class SharePointQueryOrderByClauseExtensionMethodsTest
    {
        [TestMethod]
        public void ToXElement_returns_correct_xml()
        {
            var clause = new SharePointQueryOrderByClause();
            clause.AddExpression("Title", true);
            clause.AddExpression("Description", false);
            var orderBy = clause.ToXElement();
            var list = orderBy.Descendants().Where(e => e.Name.LocalName == "FieldRef").ToList();
            Assert.AreEqual(2, list.Count);
            Assert.AreEqual("Title", list[0].Attribute("Name").Value);
            Assert.AreEqual("Description", list[1].Attribute("Name").Value);

        }

        [TestMethod]
        public void ToXElement_returns_empty_order_by_element_when_input_is_null()
        {
            SharePointQueryOrderByClause clause = null;
            var xml = clause.ToXElement();
            Assert.AreEqual("<OrderBy />", xml.ToString());
        }

        [TestMethod]
        public void ToSharePointQueryOrderByClause_returns_correct_object()
        {
            var xml = XElement.Parse("<OrderBy><FieldRef Name=\"Title\" Ascending=\"False\" /><FieldRef Name=\"Description\" Ascending=\"True\" /></OrderBy>");
            var orderBy = xml.ToSharePointQueryOrderByClause();
            Assert.AreEqual(2, orderBy.Expressions.Count);
            Assert.AreEqual("Title", orderBy.Expressions[0].FieldStaticName);
            Assert.IsFalse(orderBy.Expressions[0].SortAscending);
            Assert.AreEqual("Description", orderBy.Expressions[1].FieldStaticName);
            Assert.IsTrue(orderBy.Expressions[1].SortAscending);
        }
        
        [TestMethod]
        public void ToSharePointQueryOrderByClause_returns_correct_object_when_differs_by_case()
        {
            var xml = XElement.Parse("<OrderBy><Fieldref NAme=\"Title\" AScending=\"false\" /><FIELDREF name=\"Description\" AscendiNg=\"TRUE\" /></OrderBy>");
            var orderBy = xml.ToSharePointQueryOrderByClause();
            Assert.AreEqual(2, orderBy.Expressions.Count);
            Assert.AreEqual("Title", orderBy.Expressions[0].FieldStaticName);
            Assert.IsFalse(orderBy.Expressions[0].SortAscending);
            Assert.AreEqual("Description", orderBy.Expressions[1].FieldStaticName);
            Assert.IsTrue(orderBy.Expressions[1].SortAscending);
        }

        [TestMethod]
        public void ToSharePointQueryOrderByClause_sets_ascending_to_true_when_attribute_is_not_present()
        {
            var xml = XElement.Parse("<OrderBy><FieldRef Name=\"Title\" Ascending=\"False\" /><FieldRef Name=\"Description\" /></OrderBy>");
            var orderBy = xml.ToSharePointQueryOrderByClause();
            Assert.AreEqual(2, orderBy.Expressions.Count);
            Assert.AreEqual("Title", orderBy.Expressions[0].FieldStaticName);
            Assert.IsFalse(orderBy.Expressions[0].SortAscending);
            Assert.AreEqual("Description", orderBy.Expressions[1].FieldStaticName);
            Assert.IsTrue(orderBy.Expressions[1].SortAscending);
        }
    }
}
